package com.ekuaitu.kuaitu.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.activity.CarCertificateActivity;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.bean.CommitCertificateBean;
import com.ekuaitu.kuaitu.utils.CheckRegexUtils;
import com.ekuaitu.kuaitu.utils.Constants;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class IdcardFragment extends Fragment {


    @BindView(R.id.certificate_idName)
    EditText certificateIdName;
    @BindView(R.id.certificate_idNum)
    EditText certificateIdNum;
    @BindView(R.id.certificate_idCommit)
    TextView certificateIdCommit;
    @BindView(R.id.progressBar_idNum)
    ProgressBar progressBarIdNum;
    private Context mContext;

    public IdcardFragment() {
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getContext();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_certify_idcard, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }


    private void confirmIdCardCommit(String name, String idNum) {
        progressBarIdNum.setVisibility(View.VISIBLE);
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).confirmIdCardCommit(MyApplication.getInstance().getRequestFieldMap(),name, idNum).enqueue(new Callback<CommitCertificateBean>() {
            @Override
            public void onResponse(Call<CommitCertificateBean> call, Response<CommitCertificateBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        progressBarIdNum.setVisibility(View.GONE);

                        CarCertificateActivity activity = (CarCertificateActivity) getActivity();
                        activity.mVp.setCurrentItem(2);

                    } else {
                        progressBarIdNum.setVisibility(View.GONE);
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    progressBarIdNum.setVisibility(View.GONE);
                    ToastCompat.makeText(mContext, getResources().getString(R.string.serverError), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<CommitCertificateBean> call, Throwable t) {
                progressBarIdNum.setVisibility(View.GONE);
                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @OnClick(R.id.certificate_idCommit)
    public void onClick() {
        MobclickAgent.onEvent(this.getActivity(), Constants.REALNAME_AUTHENTICATION);
        if (!TextUtils.isEmpty(certificateIdName.getText().toString())) {
            if (!TextUtils.isEmpty(certificateIdNum.getText().toString())) {
                if (CheckRegexUtils.checkIdCard(certificateIdNum.getText().toString())) {
                    confirmIdCardCommit(certificateIdName.getText().toString(), certificateIdNum.getText().toString());
                } else {
                    ToastCompat.makeText(mContext, "请输入正确的身份证号码！", Toast.LENGTH_SHORT).show();
                }
            } else {
                ToastCompat.makeText(mContext, "请输入身份证号码！", Toast.LENGTH_SHORT).show();
            }
        } else {
            ToastCompat.makeText(mContext, "请输入姓名！", Toast.LENGTH_SHORT).show();
        }
    }
}
